namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class IndicatorRelativeStrengthIndex : BaseIndicator
    {
        private string absField = ("RSI.ABS." + Common.GetGuid());
        private string absSmaField = ("RSI.ABSSMA." + Common.GetGuid());
        private string maxField = ("RSI.MAX." + Common.GetGuid());
        private string maxSmaField = ("RSI.MAXSMA." + Common.GetGuid());
        private LineShape tlsRsi;

        public IndicatorRelativeStrengthIndex()
        {
            base.IndType = IndicatorType.RSI;
            base.IndName = "RSI";
            base.IndDesc = "相对强弱指标";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 6.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "RSI" };
        }

        public override bool Contains(object obj)
        {
            return (obj == this.tlsRsi);
        }

        public override double[] Get(int r)
        {
            double num = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double num2 = 0.0;
            if (r > 0)
            {
                num2 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][base.SourceDic["Close"]]);
            }
            double curValue = ((num - num2) >= 0.0) ? (num - num2) : 0.0;
            double num4 = Math.Abs((double) (num - num2));
            double num5 = Common.CalcuteSimpleMovingAvg(r, (int) base.IndPeriod[0].ParamValue, 1, this.maxSmaField, this.maxField, curValue, base.Crab.DataSource);
            double num6 = Common.CalcuteSimpleMovingAvg(r, (int) base.IndPeriod[0].ParamValue, 1, this.absSmaField, this.absField, num4, base.Crab.DataSource);
            if (num6 != 0.0)
            {
                return new double[] { curValue, num4, num5, num6, ((num5 / num6) * 100.0) };
            }
            return new double[] { curValue, num4, num5, num6, 100.0 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsRsi };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsRsi);
            base.Crab.DataSource.Columns.Remove(this.maxField);
            base.Crab.DataSource.Columns.Remove(this.maxSmaField);
            base.Crab.DataSource.Columns.Remove(this.absField);
            base.Crab.DataSource.Columns.Remove(this.absSmaField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        Common.GetDateTimeByTimeKey(row[base.Crab.CalenderScaleField].ToString());
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.maxField, numArray[0], r);
                        base.Crab.SetValue(this.absField, numArray[1], r);
                        base.Crab.SetValue(this.maxSmaField, numArray[2], r);
                        base.Crab.SetValue(this.absSmaField, numArray[3], r);
                        base.Crab.SetValue(this.TlsRsi.Name, numArray[4], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            base.SourceDic["Close"] = source[0];
            base.IndPeriod[0].ParamValue = periods[0];
            base.Crab.DataSource.Columns.Add(this.MaxField);
            base.Crab.DataSource.Columns.Add(this.MaxSmaField);
            base.Crab.DataSource.Columns.Add(this.AbsField);
            base.Crab.DataSource.Columns.Add(this.AbsSmaField);
            this.TlsRsi = base.Crab.AddLine(fields[0], panelID);
        }

        public string AbsField
        {
            get
            {
                return this.absField;
            }
            set
            {
                this.absField = value;
            }
        }

        public string AbsSmaField
        {
            get
            {
                return this.absSmaField;
            }
            set
            {
                this.absSmaField = value;
            }
        }

        public string MaxField
        {
            get
            {
                return this.maxField;
            }
            set
            {
                this.maxField = value;
            }
        }

        public string MaxSmaField
        {
            get
            {
                return this.maxSmaField;
            }
            set
            {
                this.maxSmaField = value;
            }
        }

        public LineShape TlsRsi
        {
            get
            {
                return this.tlsRsi;
            }
            set
            {
                this.tlsRsi = value;
            }
        }
    }
}

